package com.laikan.reader.apirequester;

import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.core.bean.AutoBuyBean;
import com.laikan.reader.core.bean.BaseDataBean;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wx on 16/9/30.
 * 章节自动购买状态获取和设置
 */
public class ChapterAutoBuy {

    private RefreshWindowOnClient refreshWindowOnClient;
    private int GETAutoStatusConstant = 1;//标识获取自动购买状态
    private int SetAutoStatusConstant = 2;//标识设置购买状态
    public ChapterAutoBuy(Context context) {
    }

    /**
     * 获取自动购买状态
     * @param sd
     * @param bookid
     */
    public void getAutoStatus(String sd, String bookid) {

        Map<String, String> map = new HashMap<>();
        map.put("sd", sd);
        map.put("bookId", bookid);
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() {
            }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                gsonnalysis(httpResponse.responseContent,GETAutoStatusConstant);

            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {

            }

            @Override
            public void onFinish() {
            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL3() + "get/autoSubscribe");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.getRootURL3() + "get/autoSubscribe", map, null, null, requestListener);

    }

    /**
     * 设置自动购买状态
     * @param sd
     * @param bookid
     * @param auto
     */
    public void setAutoStatus(String sd, String bookid, String auto) {
        Map<String, String> map = new HashMap<>();
        map.put("sd", sd);
        map.put("bookId", bookid);
        map.put("auto", auto);
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() {

            }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                gsonnalysis(httpResponse.responseContent,SetAutoStatusConstant);
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {

            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL3() + "autoSubscribe");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.getRootURL3() + "autoSubscribe", map, null, null, requestListener);
    }

    public void setRefreOnClient(RefreshWindowOnClient l) {
        this.refreshWindowOnClient = l;
    }

    /**
     * 解析
     * @param json
     * @param responseType
     */
    private void gsonnalysis(String json, int responseType) {
        BaseDataBean<AutoBuyBean> autoBuyResult;
        Gson gson = new Gson();
        Type type = new TypeToken<BaseDataBean<AutoBuyBean>>() {
        }.getType();
        try {
            autoBuyResult = gson.fromJson(json, type);
            if (autoBuyResult != null) {
                if (autoBuyResult.getError_no().equals("0")) {
                    if (refreshWindowOnClient != null) {
                        refreshWindowOnClient.refreWindow(autoBuyResult.getData().isAutoBuy(),responseType);
                    }
                }else{
                    ToastMgr.showShortToast(autoBuyResult.getError_msg());
                }
            }
        }catch (Exception e) {
        }
    }

    /**
     * 回调接口
     */
    public interface RefreshWindowOnClient {

         void refreWindow(boolean autoBuy,int type);
    }
}
